package com.jinlejia.erp.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RadioGroup;
import android.widget.TextView;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.freedom.yefeng.yfrecyclerview.YfListAdapter;
import com.freedom.yefeng.yfrecyclerview.YfSimpleViewHolder;
import com.jinlejia.erp.R;
import com.jinlejia.erp.base.ApiConfig;
import com.jinlejia.erp.databinding.ItemEntering2Binding;

import java.util.ArrayList;
import java.util.Map;

/**
 * 录带看第二步（Adapter）
 */

public class Entering2Adapter extends YfListAdapter<Map<String, Object>> {

    private Context context;

    public Entering2Adapter(ArrayList<Map<String, Object>> data) {
        super(data);
    }

    @Override
    public RecyclerView.ViewHolder onCreateDataViewHolder(ViewGroup parent) {
        context = parent.getContext().getApplicationContext();
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_entering2, parent, false);
        return new ViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateEmptyViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.empty_layout, parent, false);
        TextView txt = view.findViewById(R.id.id_tips);
        txt.setText("暂无数据");
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateErrorViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.error_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public void onBindDataViewHolder(RecyclerView.ViewHolder holder, final int position) {
        ItemEntering2Binding binding = DataBindingUtil.getBinding(holder.itemView);
        Glide.with(context).load(ApiConfig.MAGEHEADER_URL() +
                mData.get(position).get("image")).placeholder(R.mipmap.ic_no_img).error(R.mipmap.ic_no_img).into(binding.shootIcon);
        binding.address.setText(mData.get(position).get("address").toString());
        binding.data.setText(mData.get(position).get("info").toString());
        binding.money.setText(mData.get(position).get("money").toString());
        mData.get(position).put("houseType","本房");
        mData.get(position).put("feedback","有意向");
        binding.rgHouse.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.this_radio:
                        //本房
                        mData.get(position).put("houseType","本房");
                        break;
                    case R.id.this_model_radio:
                        //本户型
                        mData.get(position).put("houseType","本户型");
                        break;
                    case R.id.contra__model_radio:
                        //反户型
                        mData.get(position).put("houseType","反户型");
                        break;
                    default:
                        break;
                }
            }
        });
        binding.rgFeedback.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.mind_radio:
                        //有意向
                        mData.get(position).put("feedback","有意向");
                        break;
                    case R.id.think_of_radio:
                        //在考虑
                        mData.get(position).put("feedback","在考虑");
                        break;
                    case R.id.discontent_radio:
                        //不满意
                        mData.get(position).put("feedback","不满意");
                        break;
                    default:
                        break;
                }
            }
        });
        binding.houseItem.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onHouseItemClickListener != null)
                    onHouseItemClickListener.OnHouseItemClick( mData.get(position).get("rscode").toString());
            }
        });
        binding.getRoot().setTag(mData.get(position));
    }

    @Override
    public RecyclerView.ViewHolder onCreateLoadingViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.loading_layout, parent, false);
        return new YfSimpleViewHolder(view);
    }

    @Override
    public RecyclerView.ViewHolder onCreateFooterViewHolder(ViewGroup parent) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.footer_loading, parent, false);
        return new YfSimpleViewHolder(view);
    }


    static class ViewHolder extends RecyclerView.ViewHolder {

        ViewHolder(View view) {
            super(view);
            DataBindingUtil.bind(view);
        }
    }
    public interface OnHouseItemClickListener {
        void OnHouseItemClick(String code);
    }
    private OnHouseItemClickListener onHouseItemClickListener;
    public void setOnHouseItemClickListener( OnHouseItemClickListener listener ){
        this.onHouseItemClickListener = listener;
    }
}
